package com.zzb.weikeautosign.socket;

import com.zzb.weikeautosign.PropertiesUtil;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * socket服务器
 * Created by 瑱彬 on 2016/3/3.
 */
public class SocketService {
    private static Logger logger = Logger.getLogger(SocketService.class);
    private final int serverport = NumberUtils.toInt(PropertiesUtil.getProperties("config.properties","serverport"),29999);
    private ServerSocket serverSocket;
    private static SocketService service;

    public static SocketService getInstance() {
        if(service == null) service = new SocketService();
        return service;
    }
    /**
     * socket监听开启服务
     */
    public void execute(){
        if(serverSocket == null){
            new Thread(() -> {
                try {
                    serverSocket = new ServerSocket(serverport);
                    while (true){
                        Socket client = serverSocket.accept();
                        new ServiceThread(client).start();
                    }
                } catch (IOException e) {
                    logger.error(e.getMessage());
                }
            }).start();
        }
    }

}
